# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import pymongo


class ChoutiPipeline:
    def __init__(self):
        # 连接数据库
        self.client = pymongo.MongoClient('localhost')
        # 创建库
        self.db = self.client['chouti']
        # 创建表
        self.table = self.db['ct']

    def process_item(self, item, spider):
        msg_type =item.get('type')
        if msg_type =="info":
            item.pop("type")
            self.table.insert(item)
        else:
            item.pop("type")
            self.table.update_one({"id": item["id"]}, {"$set": {"comments": item["content"]}})
